//
//  StuffDetailViewController.m
//  Marketing
//
//  Created by HanenDev on 16/2/26.
//  Copyright © 2016年 Hanen 3G 01. All rights reserved.
//

#import "StuffDetailViewController.h"
#import "InformationCell.h"
#import "QRCodeGenerator.h"

#define cellHeight 50.0f

@interface StuffDetailViewController ()<UITableViewDataSource,UITableViewDelegate>
{
    NSArray      *_titleArray;
    UITableView  *_tableView;
    UILabel      *qrNameLabel;
    UIImageView  *_bgImage1;
    UIButton     *QRbutton;
    
}
@property(nonatomic,strong)UIView         *qrView;
@property(nonatomic,strong)UIImageView    *qrImageView;
@property(nonatomic,strong)UIImageView    *bgImage;
@property(nonatomic,strong)UIButton       *headerBtn;

@end

@implementation StuffDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = graySectionColor;
    self.navigationItem.hidesBackButton = YES;
    
    self.navigationItem.leftBarButtonItem = [ViewTool getBarButtonItemWithTarget:self WithAction:@selector(goToBack)];
    self.navigationItem.titleView = [ViewTool getNavigitionTitleLabel:@"个人信息"];
    self.navigationItem.rightBarButtonItem = [ViewTool getBarButtonItemWithTarget:self WithString:@"HY_nav_icon_QR" WithAction:@selector(getQRcode:)];
    
    _titleArray = @[@"所属部门",@"职位"];
    
    [self createUI];
    [self createQRView];
    //[self initData];
    
}
- (void)initData{
    NSNumber *cidNum = [NSNumber numberWithInt:_cID];
    NSDictionary * dic =@{@"token":TOKEN,@"uid":@(UID),@"userid":cidNum};
    [DataTool postWithUrl:STUFF_DETAIL_URL parameters:dic success:^(id data) {
        NSDictionary *jsonDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        NSLog(@"%@+++++--wodexiangqing",jsonDic);
        _nameLabel.text = jsonDic[@"name"];
        
        NSString  *imageStr =[NSString stringWithFormat:@"%@%@",LoadImageUrl,jsonDic[@"logo"]];
        
        [_headerImageView sd_setImageWithURL:[NSURL URLWithString:imageStr]];
        [_bgImage1 sd_setImageWithURL:[NSURL URLWithString:imageStr]];
        
        _phoneLabel.text = jsonDic[@"username"];
        
        
        NSString  *qrImageStr =[NSString stringWithFormat:@"%@%@",LoadImageUrl,jsonDic[@"img"]];
        _qrImageView.image = [QRCodeGenerator qrImageForString:qrImageStr imageSize:300];
        
        qrNameLabel.text = jsonDic[@"name"];
        
        [_tableView reloadData];
        
        
    } fail:^(NSError *error) {
        NSLog(@"%@",error);
    }];
    
}

- (void)createUI{
    
    CGFloat bgImageH     = [UIView getHeight:270];//背景图片的高度
    CGFloat headerImageW  = [UIView getHeight:120];//头像尺寸
    CGFloat qrSpace      = 15;
//    CGFloat qrW          = [UIView getWidth:40];
    
    UIView *headerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0-64, KSCreenW, bgImageH)];
    headerView.backgroundColor = NavBackColor;
    [self.view addSubview:headerView];
    
    UILabel *myLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, qrSpace+10, KSCreenW, 30)];
    //    myLabel.text = @"个人信息";
    myLabel.textColor = [UIColor whiteColor];
    myLabel.font = [UIFont boldSystemFontOfSize:17];
    myLabel.textAlignment = NSTextAlignmentCenter;
    [headerView addSubview:myLabel];
    
    //    _qrBtn = [[UIButton alloc]initWithFrame:CGRectMake(KSCreenW - qrW - qrSpace, qrSpace+64, qrW, qrW)];
    //    [_qrBtn setImage:[UIImage imageNamed:@"HY_nav_icon_QR"] forState:UIControlStateNormal];
    //    [_qrBtn addTarget:self action:@selector(getQRcode:) forControlEvents:UIControlEventTouchUpInside];
    //    [headerView addSubview:_qrBtn];
    
    _headerBtn = [[UIButton alloc]initWithFrame:CGRectMake((KSCreenW-headerImageW)/2, (headerView.height - headerImageW)/2, headerImageW, headerImageW)];
    _headerBtn.backgroundColor = [UIColor cyanColor];
    _headerBtn.layer.cornerRadius = headerImageW/2;
    _headerBtn.layer.masksToBounds = YES;
    _headerBtn.userInteractionEnabled = NO;
    NSString  *imageStr =[NSString stringWithFormat:@"%@%@",LoadImageUrl,_logo];
    [_headerBtn sd_setBackgroundImageWithURL:[NSURL URLWithString:imageStr] forState:UIControlStateNormal placeholderImage:[UIImage imageNamed:@"HY_btn_edit_head"]];
    [_headerBtn.layer setBorderWidth:5.0];
    [_headerBtn.layer setBorderColor:UIColorFromRGB(0x8095c4).CGColor];//边框颜色
    [headerView addSubview:_headerBtn];
    
    
    _nameLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, _headerBtn.maxY + 10, KSCreenW, 20)];
    _nameLabel.textColor = UIColorFromRGB(0xc4cbdf);
    _nameLabel.textAlignment = NSTextAlignmentCenter;
    _nameLabel.font = [UIFont systemFontOfSize:16.0f];
    _nameLabel.text = _name;
    //    _nameLabel.font = [UIFont systemFontOfSize:15];
    [headerView addSubview:_nameLabel];
    
    _phoneLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, _nameLabel.maxY +5, KSCreenW, 20)];
    _phoneLabel.textColor=UIColorFromRGB(0xc4cbdf);
    _phoneLabel.textAlignment = NSTextAlignmentCenter;
    _phoneLabel.font = [UIFont systemFontOfSize:14.0f];
    _phoneLabel.text = [NSString stringWithFormat:@"手机号:%@",_phone];
    //    _phoneLabel.font = [UIFont systemFontOfSize:15];
    [headerView addSubview:_phoneLabel];
    
    
    UIView *lineView = [ViewTool getLineViewWith:CGRectMake(0, headerView.maxY + 20 -1, KSCreenW, 1) withBackgroudColor:grayLineColor];
    [self.view addSubview:lineView];
    
    _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, lineView.maxY, KSCreenW, 2*cellHeight) style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.scrollEnabled =NO;
    
    _tableView.rowHeight = cellHeight;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _tableView.showsVerticalScrollIndicator = NO;
    [self.view addSubview:_tableView];
    
}
- (void)createQRView{
    
    UIWindow *window = [UIApplication sharedApplication].delegate.window;
    
    _qrView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, KSCreenW, KSCreenH)];
    _qrView.backgroundColor = [UIColor blackColor];
    _qrView.alpha = 0.5;
    _qrView.hidden = YES;
    [window addSubview:_qrView];
    
    _bgImage = [[UIImageView alloc]initWithFrame:CGRectMake([UIView getWidth:60], [UIView getHeight:120], KSCreenW - 2*[UIView getWidth:60], KSCreenW - 2*[UIView getWidth:60])];
    //    _bgImage.image = [UIImage imageNamed:@"二维码bg"];
    _bgImage.backgroundColor = [UIColor whiteColor];
    _bgImage.hidden = YES;
    _bgImage.layer.cornerRadius = 5;
    _bgImage.layer.masksToBounds = YES;
    [window addSubview:_bgImage];
    
    QRbutton = [[UIButton alloc]initWithFrame:CGRectMake((KSCreenW -40) / 2, _bgImage.maxY + 50, 40, 40)];
    [QRbutton setImage:[UIImage imageNamed:@"HY_btn_close"] forState:UIControlStateNormal];
    [QRbutton addTarget:self action:@selector(hiddenQRView) forControlEvents:UIControlEventTouchUpInside];
    QRbutton.hidden = YES;
    [window addSubview:QRbutton];
    
    qrNameLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, [UIView getHeight:20], KSCreenW - 2*[UIView getWidth:40], [UIView getHeight:30])];
    qrNameLabel.textColor = [UIColor whiteColor];
    qrNameLabel.textAlignment = NSTextAlignmentCenter;
    [_bgImage addSubview:qrNameLabel];
    
    _qrImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, _bgImage.width, _bgImage.height)];
    NSString  *qrImageStr =[NSString stringWithFormat:@"%@%@",LoadImageUrl,_imag];
    _qrImageView.image = [QRCodeGenerator qrImageForString:qrImageStr imageSize:300];
    [_bgImage addSubview:_qrImageView];
    
    
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(toHiddenQRView:)];
    [_qrView addGestureRecognizer:tapGesture];
    
}
//二维码扫描
- (void)getQRcode:(UIButton *)btn{
    
    [UIView animateWithDuration:0.25 animations:^{
        _qrView.hidden = NO;
        _bgImage.hidden = NO;
        QRbutton.hidden = NO;
    }];
}
- (void)hiddenQRView{
    [UIView animateWithDuration:0.25 animations:^{
        _qrView.hidden = YES;
        _bgImage.hidden = YES;
        QRbutton.hidden = YES;
    }];
}
- (void)toHiddenQRView:(UITapGestureRecognizer *)tap{
    [UIView animateWithDuration:0.25 animations:^{
        _qrView.hidden = YES;
        _bgImage.hidden = YES;
        QRbutton.hidden = YES;
    }];
}
- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    [UIView animateWithDuration:0.25 animations:^{
        _qrView.hidden = YES;
        _bgImage.hidden = YES;
    }];
}

#pragma mark
#pragma mark---------
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _titleArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *identifier = @"cell";
    InformationCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil) {
        cell=[[InformationCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.titleLabel.text = _titleArray[indexPath.row];
    if (indexPath.row == 0) {
        cell.subLabel.text =_departmentName;
        cell.lineV.frame = CGRectMake(cell.titleLabel.x, cellHeight-1, KSCreenW - cell.titleLabel.x, 1);
    }else if (indexPath.row == 1){
        cell.subLabel.text =_positionName;
        cell.lineV.frame = CGRectMake(0, cellHeight-1, KSCreenW, 1);
    }
    
    return cell;
}
- (void)goToBack{
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)viewWillDisappear:(BOOL)animated
{
    self.tabBarController.hidesBottomBarWhenPushed = NO;
}

- (void)viewWillAppear:(BOOL)animated
{
    self.tabBarController.hidesBottomBarWhenPushed = YES;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
